package com.yuanfeng.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.yuanfeng.business.dto.UserTokenBeanDTO;
import com.yuanfeng.business.entity.ShopAgreementEntity;
import com.yuanfeng.business.entity.ShopAgreementRuleEntity;
import com.yuanfeng.business.mapper.ShopAgreementMapper;
import com.yuanfeng.business.mapper.ShopAgreementRuleMapper;
import com.yuanfeng.business.service.ShopAgreementRuleService;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class ShopAgreementRuleServiceImpl extends ServiceImpl<ShopAgreementRuleMapper, ShopAgreementRuleEntity>
    implements ShopAgreementRuleService{

    @Autowired
    private ShopAgreementMapper shopAgreementMapper;

    @Override
    public ResponseResult queryAgreementContent(Map<String, Object> map) {
        String agreementContent = this.baseMapper.selectById(map.get("agreementId").toString()).getAgreementContent();
        return ResponseResult.success(agreementContent);
    }

    @Override
    public ResponseResult queryShopJoinAgreeList(UserTokenBeanDTO param) {
        UserInfoVO userInfo = ParseTokenUtils.getUserInfoByToken(param.getToken());

        List<ShopAgreementRuleEntity> agreementList = this.baseMapper.selectList(new LambdaQueryWrapper<ShopAgreementRuleEntity>());
        List<ShopAgreementEntity> shopJoinMes = shopAgreementMapper.selectList(new LambdaQueryWrapper<ShopAgreementEntity>()
                                                .eq(ShopAgreementEntity::getShopId,userInfo.getShopId()));

        for (ShopAgreementRuleEntity agreement : agreementList) {

            for (ShopAgreementEntity shopJoin : shopJoinMes) {
                if (agreement.getAgreementId().intValue() == shopJoin.getAgreementId().intValue()) {
                    agreement.setShopIsAgree(shopJoin.getShopIsAgree());
                    break;
                }
            }
        }

        if (CollectionUtils.isEmpty(agreementList)) {
            agreementList = Lists.newArrayList();
        }
        return ResponseResult.success(agreementList);
    }

    @Override
    public ResponseResult queryNewAgreement() {
        ShopAgreementRuleEntity result =  this.baseMapper.selectAgreementRule();

        if (null == result) {
            return ResponseResult.fail(0, "暂无新协议");
        }

        return ResponseResult.success(result);
    }
}




